/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.di0.uteis;

import java.awt.AWTKeyStroke;
import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.util.HashSet;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

/**
 *
 * @since 2009.08.17
 * @author di0
 */
public class MetodosUteis {

    public MetodosUteis() {
    }

    /**
     * Limpa o conteudo de todos os JTextField de um determinado JPanel
     * @param painel o JPanel a ser limpo
     */
    public void limparCampos(JPanel painel) {
        Component[] vetor = painel.getComponents();
        for (int cont = 0; cont < vetor.length; cont++) {
            if (vetor[cont] instanceof JTextField) {
                JTextField text = (JTextField) vetor[cont];
                text.setText("");
            }
            if (vetor[cont] instanceof JPanel) {
                limparCampos((JPanel) vetor[cont]);
            }
        }
    }

    /**Retorna a string no formato da mascara passada por parametro
     *
     * @param texto a String a ser formatada
     * @param mascara a mascara a ser aplicada
     * @return a String alterada
     * @throws java.text.ParseException
     */
    public String formatarString(String texto, String mascara) throws ParseException {
        MaskFormatter mf = new MaskFormatter(mascara);
        mf.setValueContainsLiteralCharacters(false);
        return mf.valueToString(texto);
    }

    /**
     * Define a tecla enter como TraversalKey
     * @param painel o painel a adicinar esta funcionalidade
     */
    public void setEnterHowNextTraversalKey(JPanel painel) {
        HashSet keys = new HashSet(
                painel.getFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

        keys.add(AWTKeyStroke.getAWTKeyStroke(
                KeyEvent.VK_ENTER, 0));

        painel.setFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys);
    }

    /**
     * Converte string timestamp do banco de dados PostgreSQL para o formato "dd/MM/yyyy"
     * @param timeStamp a string no formato timestamp
     * @return a string formatada.
     */
    public static String timeStampToString(String timeStamp) {
        java.sql.Timestamp timeStampDate = java.sql.Timestamp.valueOf(timeStamp);
        java.util.Date data = new java.util.Date(timeStampDate.getTime());
        String dataFormat = new java.text.SimpleDateFormat("dd/MM/yyyy").format(data);
        return dataFormat;
    }

    /**
     * Remove a tecla enter como TraversalKey
     * @param painel o painel a remover esta funcionalidade
     */
    public void removeEnterHowNextTraversalKey(JPanel painel) {
        HashSet keys = new HashSet(
                painel.getFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        HashSet keys2 = (HashSet) keys.clone();

        for (Object object : keys) {
            if (object.equals(AWTKeyStroke.getAWTKeyStroke(
                    KeyEvent.VK_ENTER, 0))) {
                keys2.remove(object);
            }
        }

        painel.setFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys2);
    }
}
